﻿Imports EstoqueDAL
Imports EstoqueBLL
Public Class ComprasUC
    Dim objbll As New ComprasBLL
    Dim x As String

#Region "Alterar"
    Private Sub TSBAlterar_Click(sender As System.Object, e As System.EventArgs) Handles TSBAlterar.Click
        Try
            For Each linha As DataGridViewRow In DGVDadoss.SelectedRows
                If (Not linha.Cells("com_codigo") Is Nothing) Then
                    objbll = New ComprasBLL(Val(DGVDadoss.Rows(linha.Index).Cells("com_codigo").Value.ToString()))
                End If
            Next
            Dim cad As New ComprasCAD
            If (objbll.com_codigo > 0 And objbll.sta_codigo = 4) Then
                cad.LCodigo.Text = objbll.com_codigo
                cad.Op = "E"
                cad.LCodigo.Visible = False
                TSBAlterar.BackColor = Color.Gainsboro
                cad.ShowDialog()
                CBFiltro_SelectedIndexChanged(Nothing, Nothing)
            Else
                MessageBox.Show("A compra já foi fechada e não pode ser alterada.", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
                x = "I"
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Erro:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Finally
            TSBAlterar.BackColor = Color.Transparent
        End Try
    End Sub
#End Region

#Region "Cadastrar"
    Private Sub TSBCadastrar_Click(sender As System.Object, e As System.EventArgs) Handles TSBCadastrar.Click
        Try
            Dim cad As New ComprasCAD
            TSBCadastrar.BackColor = Color.Gainsboro
            cad.Op = "I"
            cad.ShowDialog()
            CBFiltro_SelectedIndexChanged(Nothing, Nothing)
            TSBCadastrar.BackColor = Color.Transparent
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Erro:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Finally
            TSBCadastrar.BackColor = Color.Transparent
        End Try
    End Sub
#End Region

#Region "Carrega Grid"
    Public Sub carregagrid()
        Dim dt As New DataTable
        dt = objbll.localizar(Nothing, 90, Nothing)
        DGVDadoss.DataSource = dt
        DGVDadoss.Refresh()

    End Sub
#End Region

#Region "Click da Grid"
    Private Sub DGVDados_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVDadoss.CellClick
        objBLL = Nothing
        If (e.RowIndex >= 0) Then
            objbll = New ComprasBLL(Val(DGVDadoss.Rows(e.RowIndex).Cells(0).Value))
        End If
    End Sub
#End Region

#Region "CBFiltro selectedindex"
    Private Sub CBFiltro_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBFiltro.SelectedIndexChanged
        txtdescricao.Clear()
        If txtdescricao.Text = "" Then
            carregagrid()
        End If
        Select Case CBFiltro.Text
            Case "Status"
                cbstatus.Visible = True
                txtdescricao.Visible = False
                dt1.Visible = False
                dt2.Visible = False
                ltexto.Visible = False
                CBStatus_SelectedIndexChanged(Nothing, Nothing)
                blocalizar.Visible = False
            Case "Data Pedido"
                cbstatus.Visible = False
                txtdescricao.Visible = False
                dt1.Visible = True
                dt2.Visible = True
                ltexto.Visible = True
                blocalizar.Visible = True
            Case "Data Chegada"
                cbstatus.Visible = False
                txtdescricao.Visible = False
                dt1.Visible = True
                dt2.Visible = True
                ltexto.Visible = True
                blocalizar.Visible = True
            Case "Data Emissão"
                cbstatus.Visible = False
                txtdescricao.Visible = False
                dt1.Visible = True
                dt2.Visible = True
                ltexto.Visible = True
                blocalizar.Visible = True
            Case "Fornecedor"
                cbstatus.Visible = False
                txtdescricao.Visible = True
                dt1.Visible = False
                dt2.Visible = False
                ltexto.Visible = False
                blocalizar.Visible = False
        End Select

    End Sub
#End Region

#Region "Combobox Status"
    Private Sub CBStatus_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbstatus.SelectedIndexChanged
        Dim sta As Integer = 0
        If (cbstatus.Text = "Aberta") Then sta = 4
        If (cbstatus.Text = "Finalizada") Then sta = 6
        DGVDadoss.DataSource = objbll.localizar(sta, 2, Nothing)
        DGVDadoss.Refresh()
    End Sub
#End Region

#Region "Excluir"
    Private Sub TSBExcluir_Click(sender As System.Object, e As System.EventArgs) Handles TSBExcluir.Click
        Try
            For Each linha As DataGridViewRow In DGVDadoss.SelectedRows
                If (Not linha.Cells("com_codigo") Is Nothing) Then
                    objbll = New ComprasBLL(Val(DGVDadoss.Rows(linha.Index).Cells("com_codigo").Value.ToString()))
                End If
            Next
            If (objbll.com_codigo > 0 And objbll.sta_codigo = 4) Then
                TSBExcluir.BackColor = Color.Gainsboro
                If (MessageBox.Show("Confirma a exclusão da compra? ", "Exclusão", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes) Then
                    objbll.excluir(objusuarios.Codigo)
                    carregagrid()
                End If
            Else
                MessageBox.Show("A compra já foi fechada e não pode ser excluída.", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Erro:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Finally
            TSBExcluir.BackColor = Color.Transparent
        End Try
    End Sub
#End Region

#Region "KeyDown Form"
    Private Sub ComprasUC_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If (e.KeyCode = Keys.Enter) Then
            SendKeys.Send("{TAB}")
        End If
        If (e.KeyCode = Keys.Escape) Then
            TSBVoltar_Click(Nothing, Nothing)
        End If
    End Sub
#End Region

#Region "Localizar"
    Private Sub TSBBuscar_Click(sender As System.Object, e As System.EventArgs) Handles TSBBuscar.Click
        If (PLocalizar.Visible) Then
            PLocalizar.Visible = False
            TSBBuscar.BackColor = Color.Transparent
            carregagrid()
        Else
            PLocalizar.Visible = True
            TSBBuscar.BackColor = Color.Gainsboro
            CBStatus.SelectedValue = 0
            CBFiltro.SelectedValue = 0
        End If
    End Sub
#End Region

#Region "Localizar Textbox"
    Private Sub TXTLocalizar_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtdescricao.TextChanged
        If (CBFiltro.Text = "Fornecedor") Then
            DGVDadoss.DataSource = objbll.localizar(txtdescricao.Text, 6, Nothing)
        End If
    End Sub
#End Region

#Region "Load"
    Private Sub CompraUC_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        carregagrid()
        cbstatus.SelectedIndex = 0
        CBFiltro.SelectedIndex = 0
        CBFiltro_SelectedIndexChanged(Nothing, Nothing)
        CBFiltro.Focus()
    End Sub
#End Region

#Region "Voltar"
    Private Sub TSBVoltar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSBVoltar.Click
        statustabestoque = "N"
        Dispose()
    End Sub
#End Region

#Region "Cor de fundo do datagridview"
    Private Sub DGVDados_RowPrePaint(sender As System.Object, e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DGVDadoss.RowPrePaint
        If DGVDadoss.Rows(e.RowIndex).Index Mod (2) = 1 Then
            DGVDadoss.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Gainsboro
        End If
    End Sub
#End Region

#Region "Localizar Botão"
    Private Sub blocalizar_Click(sender As System.Object, e As System.EventArgs) Handles blocalizar.Click
        Select Case CBFiltro.Text
            Case "Data Pedido"
                '4
                DGVDadoss.DataSource = objbll.localizar(dt1.Text, 4, dt2.Text)
            Case "Data Chegada"
                '5
                DGVDadoss.DataSource = objbll.localizar(dt1.Text, 5, dt2.Text)
            Case "Data Emissão"
                '3
                DGVDadoss.DataSource = objbll.localizar(dt1.Text, 3, dt2.Text)

        End Select
    End Sub
#End Region

#Region "Visualizar"
    Private Sub TSBVisualizar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVisualizar.Click
        Try
            For Each linha As DataGridViewRow In DGVDadoss.SelectedRows
                If (Not linha.Cells("com_codigo") Is Nothing) Then
                    objbll = New ComprasBLL(Val(DGVDadoss.Rows(linha.Index).Cells("com_codigo").Value.ToString()))
                End If
            Next
            Dim cad As New ComprasCAD
            If (objbll.com_codigo > 0) Then
                cad.LCodigo.Text = objbll.com_codigo
                cad.Op = "E"
                cad.LCodigo.Visible = False
                TSBVisualizar.BackColor = Color.Gainsboro
                cad.TSBGravar.Enabled = False
                cad.TSBAdiconar.Enabled = False
                cad.TSBEditar.Enabled = False
                cad.TSBExcluir.Enabled = False
                cad.ShowDialog()
                CBFiltro_SelectedIndexChanged(Nothing, Nothing)
            Else
                MessageBox.Show("Selecione um item para vizualizar.", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
            TSBVisualizar.BackColor = Color.Transparent

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Erro:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Finally
            TSBAlterar.BackColor = Color.Transparent
        End Try
    End Sub
#End Region

#Region "DatagridView Alterar com duplo click"
    Private Sub DGVDado_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVDadoss.CellDoubleClick
        TSBAlterar_Click(Nothing, Nothing)
        If (x = "I") Then
            TSBVisualizar_Click(Nothing, Nothing)
        End If
    End Sub
#End Region

#Region "DatagridView Deletar"
    Private Sub DGVDado_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles DGVDadoss.KeyDown
        If (e.KeyCode = Keys.Delete) Then
            TSBExcluir_Click(Nothing, Nothing)
        End If
    End Sub
#End Region

#Region "DatagridView Alterar com enter"
    Private Sub DGVDado_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles DGVDadoss.KeyPress
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
            TSBAlterar_Click(Nothing, Nothing)
        End If
        If (x = "I") Then
            TSBVisualizar_Click(Nothing, Nothing)
        End If
    End Sub
#End Region

#Region "TSB relatorio"
    Private Sub TSBRelatorio_Click(sender As System.Object, e As System.EventArgs) Handles TSBRelatorio.Click
        For Each linha As DataGridViewRow In DGVDadoss.SelectedRows
            If (Not linha.Cells("com_codigo") Is Nothing) Then
                objbll = New ComprasBLL(Val(DGVDadoss.Rows(linha.Index).Cells("com_codigo").Value.ToString()))
            End If
        Next
        If (Not IsNothing(objBLL)) Then
            If (objbll.com_codigo > 0) Then gerarelatrio("Compra: " & objbll.com_codigo, "{vw_estcomprasitens.com_codigo}=" & objbll.com_codigo)
        End If
    End Sub
#End Region

#Region "Relatorio"
    Public Sub gerarelatrio(titulo As String, formula As String)
        Try
            Dim frmrecursos As New RelatoriosFRM
            Dim rpt As New ComprasREL

            Try
                rpt.SetDatabaseLogon(usuarios, senha)
                rpt.SetParameterValue(0, titulo)
                rpt.RecordSelectionFormula = formula
                frmrecursos.CrystalReportViewer1.ShowGroupTreeButton = False
                frmrecursos.CrystalReportViewer1.ReportSource = rpt
                frmrecursos.ShowDialog()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
            frmrecursos.Dispose()
            rpt = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Finally

        End Try
    End Sub
#End Region
End Class
